package com.herench.bunch.lab.convert;

import ca.uhn.hl7v2.model.GenericPrimitive;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.Varies;
import ca.uhn.hl7v2.model.v24.group.MFN_M04_MF_CDM;
import ca.uhn.hl7v2.model.v24.message.MFK_M01;
import ca.uhn.hl7v2.model.v24.message.MFN_M04;
import ca.uhn.hl7v2.model.v24.segment.MFE;
import ca.uhn.hl7v2.model.v24.segment.MFI;
import ca.uhn.hl7v2.model.v24.segment.MSA;
import ca.uhn.hl7v2.model.v24.segment.MSH;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author luoxiaoming on 18-10-16 上午10:30
 * @email luoxiaoming@herench.com
 */
public class LabRequestBillConvert extends AbstractMessageConvert {

    @Override
    public Map<String, Object> toMessage(RequestMessage requestMessage) throws Exception {
        Map<String, Object> params = new HashMap<>();

        MFN_M04 hl7Message = (MFN_M04) requestMessage.getHl7Message();
        MFN_M04_MF_CDM mfCdm = hl7Message.getMF_CDM();
        MFE mfe = mfCdm.getMFE();
        int k = mfe.getMfe4_PrimaryKeyValueMFEReps();
        Varies[] varies = mfe.getMfe4_PrimaryKeyValueMFE();

        for(int j=0;j<k;j++){
            GenericPrimitive primitive = (GenericPrimitive) varies[j].getData();
            String[] temp = primitive.getValue().split("=");
            if (temp.length>1){
                params.put(temp[0],temp[1] != null ? temp[1] : "");
            }else {
                params.put(temp[0],"");
            }
        }
        logger.info(requestMessage.getMsgCode() + " map to string = " + params);
        return params;
    }

    @Override
    public Message fromMessage(Object message) throws Exception {
        Assert.notNull(message, "message 为空.");
        Map<String, Object> res = (Map<String, Object>) message;
        MFK_M01 mfk = new MFK_M01();
        mfk.initQuickstart("MFK_M01", "MFK_M01", "A");
        MSH msh = mfk.getMSH();
        initResponseMessageMSH(msh);
        MSA msa = mfk.getMSA();
        String returnCode = (String) res.get("returnCode");
        String returnText = (String) res.get("returnText");
        if ("0".equals(returnCode)) {
            msa.getErrorCondition().getCe1_Identifier().setValue(returnCode);
            msa.getErrorCondition().getCe2_Text().setValue("操作成功,调用HIS接口返回:execStat:" + returnText);
        } else {
            msa.getErrorCondition().getCe1_Identifier().setValue("-1");
            msa.getErrorCondition().getCe2_Text().setValue("操作失败,调用HIS接口返回:execStat:" + returnText);
        }
        return mfk;
    }



    @Override
    public Message generatorReqMessage() throws Exception {
        MFN_M04 message = new MFN_M04();
        message.initQuickstart("MFN_M04", "MFN_M04", "A");
        MSH msh = message.getMSH();
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("300030");
        msh.getMsh11_ProcessingID().getPt2_ProcessingMode().setValue(IdGenerator.getUUID());
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("SSM");
        msh.getMsh7_DateTimeOfMessage().getTs1_TimeOfAnEvent().setValue(new Date());
        msh.getMsh10_MessageControlID().setValue(IdGenerator.getUUID());
        MFI mfi = message.getMFI();

        mfi.getMasterFileIdentifier().getIdentifier().setValue("CLINIC");
        mfi.getMfi2_MasterFileApplicationIdentifier().getHd1_NamespaceID().setValue("SSM");
        mfi.getFileLevelEventCode().setValue("REGISTER");
        mfi.getEnteredDateTime().getTs1_TimeOfAnEvent().setValue(new Date());
        mfi.getMfi5_EffectiveDateTime().getTs1_TimeOfAnEvent().setValue(new Date());
        MFN_M04_MF_CDM mfCdm = message.getMF_CDM();
        MFE mfe = mfCdm.getMFE();
        mfe.getRecordLevelEventCode().setValue("ORDER");
        mfe.getEffectiveDateTime().getTs1_TimeOfAnEvent().setValue(new Date());


        /**
         * patientId
         */
        addGenericPrimitive(mfe,"patientId", "80036190");

        /**
         *身份证号
         */
        addGenericPrimitive(mfe,"orderId", "100610000002D");

        /**
         * 医保卡卡号
         */
        addGenericPrimitive(mfe,"examAccNo", "Z100610000002D");


        return message;
    }
}
